<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class Admin extends Admin_Controller {
	
	function __construct(){
	 	parent::__construct();
	 	$this->admin_navigation->parent_active('system');
	 }
	
	function index(){
		if (!$this->admin_model->logged_in()) {
			$this->_redirect(admin_url('*/index/index')); 	
		}
		$this->_redirect(admin_url('*/dashboard/index'));
	 }
	 
	function admin_manager(){
		$this->load->model('admin/admin_model');
		$columns = array(
						array(
							'name' => 'ID',
							'type' => 'id',
							'index'=>'admin_id',
							'sort_column'=>true
							),
						array(
							'name'=>$this->__('Name'),
							'type'=>'text',
							'filter'=>'fullname',
							'index'=>'fullname',
							'sort_column'=>true
						),

						array(
							'name'=>$this->__('Created'),
							'type'=>'date',
							'index'=>'created',
						),
						array(
							'name'=>$this->__('Status'),
							'type'=>'publish',
							'filter'=>'status',
							'index'=>'status',
							'options'=>array(
											 '0'=>'Tắt',
											 '1'=>'Bật'
											)
						),
		);
		$this->grid->columns($columns);
		$this->grid->datasource('admin/admin_model','get_admins');
		$this->grid->set_title($this->__('admin Manager'));
		$this->grid->add_button($this->__('Add New'),admin_url('*/admin/add_new'),'add');
		$this->grid->set_row_action('edit_admin');

		$this->grid->total_rows($this->admin_model->get_count());
		$this->grid->add_mass_action('delete', array(
			'label'=>$this->__('Delete'),
			'url'      => admin_url('*/admin/delete_admin'),
            'confirm'  =>$this->__('Are you sure ?')
		));
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function add_new(){
		$this->_forward('edit_admin');
	}
	
	function edit_admin(){
		$admin_id = $this->input->get_param('id',false);
		$this->load->model('admin/admin_model');
		if ($admin_id){
			$data = $this->admin_model->get_admin($admin_id);
			//var_dump($data);
			$this->form->set_values($data);
		}
		
		$this->form->set_form_attributes(array(
			'id'=>'admin_form',
			'action'=>admin_url('*/admin/edit_post_admin',array('id'=>$admin_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array(
			'legend'=>'Ganeral'
		));
		
		$this->form->add_filed('fullname','text',array(
			'label'=>$this->__('Full Name'),
			'name'=>'fullname',
			'required'=>true
		));
		
		$this->form->add_filed('email','text',array(
			'label'=>$this->__('Email'),
			'name'=>'email',
			'required'=>true
		));

		$this->form->add_filed('username','text',array(
			'label'=>__('User Name'),
			'name'=>'username',
			'required'=>true
		));
		
		$this->form->add_filed('password','password',array(
			'label'=>__('Password'),
			'name'=>'password',
			'required'=>true
		));
		
		$this->form->add_filed('password_confirmation','password',array(
			'label'=>__('Password Confirmation'),
			'name'=>'password_confirmation',
			'required'=>true
		));
		
		$this->form->add_filed('status','select',array(
			'label'=>$this->__('Status'),
			'name'=>'status',
			'values'=>array('Tắt','Bật')
		));
		
		$this->load->view('admin/form',array('form'=>$this->form->to_html()));
	}
	
	function edit_post_admin(){
		$this->load->model('admin/admin_model');
		$admin_id = $this->input->get_param('id',false);
		if ($admin_id) {
			$this->admin_model->update('');
		}
	}
}